package com.zsguan.z.zhttp4andr;

import android.graphics.Bitmap;
import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by chshqiang on 15-9-12.
 * Weibo Parameters
 */
public class ZParameters {
    private LinkedHashMap<String, Object> mParams = new LinkedHashMap<>();

    public ZParameters() {

    }

    public LinkedHashMap<String, Object> getParams() {
        return this.mParams;
    }

    public void setParams(Map<String, String> params) {
        this.mParams.putAll(params);
    }

    public void put(String key, String val) {
        this.mParams.put(key, val);
    }

    public void put(String key, int value) {
        this.mParams.put(key, String.valueOf(value));
    }

    public void put(String key, long value) {
        this.mParams.put(key, String.valueOf(value));
    }

    public void put(String key, Bitmap bitmap) {
        this.mParams.put(key, bitmap);
    }

    public void put(String key, Object val) {
        this.mParams.put(key, val.toString());
    }

    public Object get(String key) {
        return this.mParams.get(key);
    }

    public void remove(String key) {
        if(this.mParams.containsKey(key)) {
            this.mParams.remove(key);
        }

    }

    public Set<String> keySet() {
        return this.mParams.keySet();
    }

    public boolean containsKey(String key) {
        return this.mParams.containsKey(key);
    }

    public boolean containsValue(String value) {
        return this.mParams.containsValue(value);
    }

    public int size() {
        return this.mParams.size();
    }

    public String encodeUrl() {
        StringBuilder sb = new StringBuilder();
        boolean first = true;

        for (String key : this.mParams.keySet()) {
            if (first) {
                first = false;
            } else {
                sb.append("&");
            }

            Object value = this.mParams.get(key);
            if (value instanceof String) {
                String param = (String) value;
                if (!TextUtils.isEmpty(param)) {
                    try {
                        sb.append(URLEncoder.encode(key, "UTF-8")).append("=").append(URLEncoder.encode(param, "UTF-8"));
                    } catch (UnsupportedEncodingException var8) {
                        var8.printStackTrace();
                    }
                }

            }
        }

        return sb.toString();
    }

}
